import {
  _decorator,
  AnimationClip,
  Color,
  Component,
  Node,
  Sprite,
  UITransform,
} from "cc";
import { Communicate } from "../../Test1/Comunicate/Communicate";
import { EventType } from "../../Test1/Enums/EnumPool";
import { AnimationTest2 } from "../Animations/AnimationTest2";
import { STEADY_VALUE } from "../../Test1/Kits/SteadyValues";
import { Kit } from "../../Test1/Kits/Kit";
import { InputKey } from "../KeyEvents/InputKey";
const { ccclass, property } = _decorator;

@ccclass("Test2")
export class Test2 extends Component {
  assetFolderUrl: string = "sports/000";
  playerTest2Node: Node;
  comm: Communicate;

  displayAnimation() {
    let testComp = this.playerTest2Node.getComponent(AnimationTest2);

    testComp.assetsFolderPath = this.assetFolderUrl;
    testComp.clipWrapMode = AnimationClip.WrapMode.Normal;
    testComp.clipDuration = 2;
    testComp.playingAnim();
  }

  ctrlByBtn(data?: string) {
    let test2Component: AnimationTest2 =
      this.playerTest2Node.getComponent(AnimationTest2);
    test2Component.executePlay();
  }

  async getImagesAsset() {
    const kit = Kit.getInstance();
    let res = await kit.loadImages(this.assetFolderUrl);
    return res;
  }

  async surfaceSetting() {
    const spriteComponent = this.playerTest2Node.getComponent(Sprite);
    const transform = this.playerTest2Node.getComponent(UITransform);

    const newColor = new Color(255, 255, 255);
    spriteComponent.color = newColor;

    // 设置宽高
    transform.setContentSize(
      STEADY_VALUE.FIRST.WIDTH,
      STEADY_VALUE.FIRST.HEIGHT
    );

    let res = await this.getImagesAsset();
    spriteComponent.spriteFrame = res[0];
  }

  initializationNode() {
    let stageTest2Node = new Node("StageTest2Node");
    this.playerTest2Node = new Node("PlayerTest2Node");

    stageTest2Node.addChild(this.playerTest2Node);
    this.node.addChild(stageTest2Node);

    this.playerTest2Node.addComponent(AnimationTest2);
    this.playerTest2Node.addComponent(UITransform);
    this.playerTest2Node.addComponent(Sprite);
    this.playerTest2Node.addComponent(InputKey);
  }

  bindInputKey() {
    let input = this.playerTest2Node.getComponent(InputKey);
    input.registerMethod(this.playerTest2Node);
  }

  start() {
    console.log(this.node);
    this.surfaceSetting();
    this.displayAnimation();
    this.bindInputKey();
  }

  protected onLoad(): void {
    this.initializationNode();
    this.comm = Communicate.getInstance;
    this.comm.on(EventType.IDENTIFY_SPRINT, this.ctrlByBtn, this);
  }
}
